/*
 * This file is part of Catfish.
 * Copyright (C) 2010 Namazu Studios LLC
 * 
 * Catfish is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of 
 * the License, or (at your option) any later version.
 * 
 * Catfish is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with Catfish.  If not, please visit:
 *  
 * http://www.gnu.org/licenses/
 *  
 */
package com.namazustudios.catfish;

import java.util.List;
import java.util.Map;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Transaction;

/**
 * Interface to a source of data. This can implement a number of policies such
 * as caching, pooling, or delayed puts/deletes from the data store.
 * 
 * The definitions of this interface are intentionally left vague such that the
 * implementing modules may perform domain-specific actions 
 * 
 * @author patricktwohig
 * 
 */
public interface DataSource {

	/**
	 * Gets a single entity from the data source.
	 * 
	 * @param key
	 *            the key of the entity
	 * @return the entity itself
	 */
	public abstract Entity get(Key key) throws EntityNotFoundException;

	/**
	 * Gets several entities from the data source. This may return an empty map
	 * if the keys are not found in the data source.
	 * 
	 * @return a mapping of keys requested to the entities returned
	 */
	public abstract Map<Key, Entity> get(Key... keys);

	/**
	 * Gets several entities from the data source. This may return an empty map
	 * if the keys are not found in the data source.
	 * 
	 * @param keys
	 *            the keys to fetch
	 * 
	 * @return a mapping of keys requested to the entities returned
	 */
	public abstract Map<Key, Entity> get(Iterable<Key> keys);

	/**
	 * Gets a single entity from the data source.
	 * 
	 * @param txn
	 *            the transaction to use
	 * @param key
	 *            they key
	 * 
	 * @return an entity
	 */
	public abstract Entity get(Transaction txn, Key key)
			throws EntityNotFoundException;

	/**
	 * Gets several entities from the data source.
	 * 
	 * @return
	 */
	public abstract Map<Key, Entity> get(Transaction txn, Key... keys);

	/**
	 * Gets several entities from the data source.
	 * 
	 * @param keys
	 * @return
	 */
	public abstract Map<Key, Entity> get(Transaction txn, Iterable<Key> keys);

	/**
	 * Puts several entities into the data source.
	 * 
	 * @param entity
	 */
	public abstract List<Key> put(Entity... entities);

	/**
	 * Puts several entities into the data source.
	 * 
	 * @param entity
	 */
	public abstract List<Key> put(Iterable<Entity> entities);

	/**
	 * Puts several entities into the data source.
	 * 
	 * @para entity
	 */
	public abstract List<Key> put(Transaction txn, Entity... entities);

	/**
	 * Puts several entities into the data source.
	 * 
	 * @param entity
	 */
	public abstract List<Key> put(Transaction txn, Iterable<Entity> entities);

	/**
	 * Deletes the entities with the given keys form the data source.
	 * 
	 * @param key
	 */
	public abstract void delete(Key... keys);

	/**
	 * Deletes the entities with the given keys form the data source.
	 * 
	 * @param key
	 */
	public abstract void delete(Iterable<Key> keys);

	/**
	 * Deletes the entities with the given keys form the data source.
	 * 
	 * @param key
	 */
	public abstract void delete(Transaction txn, Key... keys);

	/**
	 * Deletes the entities with the given keys form the data source.
	 * 
	 * @param key
	 */
	public abstract void delete(Transaction txn, Iterable<Key> keys);

}
